package com.vichat.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.util.*;
import java.util.List;

/**
 *
 * @author ranjx
 *
 */
public class MapTools {
    private static transient Log logger = LogFactory.getLog(MapTools.class);
    /**
     * 返回一个点是否在一个多边形区域内
     * @param point
     * @param polygon
     * @return
     */
    public static boolean checkWithJdkGeneralPath(Point2D.Double point, List<Point2D.Double> polygon) {
        GeneralPath p = new GeneralPath();
        Point2D.Double first = polygon.get(0);
        p.moveTo(first.x, first.y);
        polygon.remove(0);
        for (Point2D.Double d : polygon) {
            p.lineTo(d.x, d.y);
        }
        p.lineTo(first.x, first.y);
        p.closePath();
        return p.contains(point);
    }

    public static void main(String[]args){
        MapTools mapTools = new MapTools();
        List<Point2D.Double> polygon = new ArrayList<>();
        double x1 = 121.47668;
        double y1 = 31.239189;
        Point2D.Double point1 = new Point2D.Double(x1,y1);
        polygon.add(point1);

        double x2 = 121.476629;
        double y2 = 31.239765;
        Point2D.Double point2 = new Point2D.Double(x2,y2);
        polygon.add(point2);

        double x3 = 121.478292;
        double y3 = 31.24021;
        Point2D.Double point3 = new Point2D.Double(x3,y3);
        polygon.add(point3);

        double x4 = 121.47855;
        double y4 = 31.240068;
        Point2D.Double point4 = new Point2D.Double(x4,y4);
        polygon.add(point4);

        double x5 = 121.478689;
        double y5 = 31.23998;
        Point2D.Double point5 = new Point2D.Double(x5,y5);
        polygon.add(point5);

        double x6 = 121.478931;
        double y6 = 31.239201;
        Point2D.Double point6 = new Point2D.Double(x6,y6);
        polygon.add(point6);

        double x7 = 121.477836;
        double y7 = 31.238902;
        Point2D.Double point7 = new Point2D.Double(x7,y7);
        polygon.add(point7);

        double x8 = 121.476796;
        double y8 = 31.238696;
        Point2D.Double point8 = new Point2D.Double(x8,y8);
        polygon.add(point8);

        //121.478885,31.239891
        //121.477973,31.239593
        //121.477855,31.239226
        double x = 121.477855;
        double y = 31.239226;
        Point2D.Double point = new Point2D.Double(x,y);
        System.out.println(mapTools.checkWithJdkGeneralPath(point,polygon));
    }
}
